<?php
class Login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function validate()
    {
        $this->load->model('member_model');
        $query = $this->member_model->validate();

        if ($query->num_rows == 1) //User found in database.
        {
            $user_data = $query->row();
            if($user_data->blocked == '1' && $user_data->admin != '1'){
                die('You have been blocked!');
            }
            if ($user_data->approved != '1' && $user_data->admin != '1') //Block when not approved and not a admin.
                die('Manager approval pending, visit later!');

            if($user_data->point < 0 && $user_data->admin != '1'){
                $username = $user_data->username;
                $this->db->query("UPDATE `user_info` SET `blocked` = 1 WHERE `username` = '$username' LIMIT 1");
                die('You have negative points, your account will be blocked!');
            }
            
            $data = array(
                'username' => $user_data->username,
                'userid' => $user_data->id,
                'is_logged_in' => TRUE,
                'user_type' => $user_data->admin
            );



            $this->session->set_userdata($data);
            redirect('user_cp');
        }
        else
        {
            echo 'Wrong';
        }
    }

    public function reg()
    {
        $_css = '<link href="' . base_url() . 'css/reg.css" rel="stylesheet" type="text/css" />';
        $_title = 'Registration';

        $data['main_content'] = 'reg_view';
        $data['h_data']['page_title'] = $_title;
        $data['h_data']['_css'] = $_css;
        $this->load->view('includes/template', $data);
    }

    public function do_reg()
    {
        $this->load->library('form_validation');

        // field name, error message, validation rules
        //        $this->form_validation->set_rules('reg_first_name', 'First Name', 'trim|required');
        //        if ($this->form_validation->run() == FALSE) {
        //            $this->load->view('reg_view');
        //        }
        //        else
        //        {
        $this->load->model('member_model');
        if ($this->member_model->create_member()) {
            echo 'Success! Please login to access your control panel.';
        }
        else
        {
            echo 'Failed! Some error has occur, please try again.';
        }
        //        }
    }

    public function logout()
    {
        //Some clear up
        $user_status = array('username' => '', 'is_logged_in' => '', 'user_type' => '', 'user_id' => '', 'pass_cap'=>FALSE);
        $this->session->unset_userdata($user_status);

        echo 'Done!';
    }

    public function update_prof()
    {
        $data = array(
            'email' => $this->input->post('reg_email'),
            'first_name' => $this->input->post('reg_first_name'),
            'last_name' => $this->input->post('reg_last_name'),
            'age' => $this->input->post('reg_age'),
            'school' => $this->input->post('reg_school')
        );
        $pw = $this->input->post('reg_pw');
        $pw2= $this->input->post('reg_pw2');
        if(!empty($pw) && ($pw == $pw2))
        {
            $data['password'] = $pw;
        }

        $this->db->update('user_info', $data);

        if($this->db->affected_rows()){
            echo 'Success!';
        }else{
            echo 'Failed!';
        }
        echo '  <br/>
                <a href="' . site_url('user_cp?action=cp_editProf') . '">Redirect in 10 seconds.</a>
                <script type="text/javascript">
                setTimeout(function() {
                    window.location = "' . site_url('user_cp?action=cp_editProf') . '"
                }, 10000);
                </script>';
    }
}
